#ifndef     __DRAW_TASK_H
#define     __DRAW_TASK_H

#include "../../rtos/thread.h"
#include "../../rtos/sync_value.h"
#include "../../utils/object.h"

namespace task {

class DrawTask : public rtos::Thread<DrawTask, 1024, osPriorityHigh1> {

public:
    enum class State : uint8_t {
        IDLE = 0,
        WORK = 1,
        STOP = 2,
    };

    enum class Bucket : uint8_t {
        ID1 = 0,
        ID2 = 1,
    };

    static constexpr uint32_t DRAW_TIMEOUT = 180 * 1000;

    void run();

    inline uint8_t state() { return static_cast<uint8_t>(m_state); }

    inline void draw_auto() {
        m_sync.set(State::IDLE);
    }
    inline void draw_stop() {
        m_sync.set(State::STOP);
    }

private:
    void open();
    void close();
    void change();

    bool wait(uint32_t timeout = osWaitForever) { return m_sync.get(m_state, timeout); }

    void do_idle();
    void do_work();
    void do_stop();

    rtos::SyncValue<State> m_sync;
    State m_state = State::IDLE;
    Bucket m_bucket = Bucket::ID2;
};

inline utils::Object<DrawTask> drawTask;

}


#endif
